package com.luoxiang.socialandroid.activity

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import com.blankj.utilcode.util.LogUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions
import com.google.gson.Gson
import com.luoxiang.lib_common.bean.NumberBean
import com.luoxiang.lib_common.common.Constants
import com.luoxiang.lib_common.utils.MmkvUtil
import com.luoxiang.socialandroid.R
import com.luoxiang.socialandroid.app.MyAppliaction
import com.luoxiang.socialandroid.base.BaseActivity
import com.luoxiang.socialandroid.bean.RandomOnlineBean
import com.luoxiang.socialandroid.databinding.ActivityOnlineMatchingBinding
import com.luoxiang.socialandroid.utils.AESUtil
import com.luoxiang.socialandroid.utils.CustomClickListener
import com.luoxiang.socialandroid.utils.NetUtils
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlin.random.Random

class OnlineMatchingActivity : BaseActivity() {
    private lateinit var mViewBinding: ActivityOnlineMatchingBinding

    override fun initLayout(savedInstanceState: Bundle?) {
        mViewBinding = ActivityOnlineMatchingBinding.inflate(layoutInflater)
        setContentView(mViewBinding.root)
        setStatusBarTranslucent()
        Glide.with(this)
            .load(R.drawable.icon_online_matching_bg)
            .into(mViewBinding.tvMatchingFengmian)
    }

    @SuppressLint("SetTextI18n")
    override fun initView() {
        lifecycleScope.launch {
            var index = 0
            while (true) {
                delay(1000)
                mViewBinding.textTitle.text = when (index) {
                    0 -> {
                        index = 1
                        getString(R.string.mi_zhengzaipipeizhong_1)
                    }
                    1 -> {
                        index = 2
                        getString(R.string.mi_zhengzaipipeizhong_2)
                    }
                    2 -> {
                        index = 0
                        getString(R.string.mi_zhengzaipipeizhong_3)
                    }
                    else -> {
                        index = 0
                        getString(R.string.mi_zhengzaipipeizhong_3)
                    }
                }
            }
        }
        lifecycleScope.launch {
            var countDown = 60
            while (countDown >= 0) {
                mViewBinding.textCountdown.text = "${countDown}s"
                delay(1000)
                countDown -= 1
                if (countDown < 0) {
                    //todo 匹配失败 跳转失败页
                }
            }
        }
        getonlineMatchingList()
        mViewBinding.imageView7.setOnClickListener(object : CustomClickListener() {
            override fun onSingleClick() {
                finish()
            }

            override fun onFastClick() {
                TODO("Not yet implemented")
            }
        })
    }

    /**
     * 请求随机获取在线用户
     */
    fun getonlineMatchingList() {
        NetUtils.getInstance().apiService.getonlineMatchingList(
            Constants.MmkvConst.EXTRA,
            MmkvUtil.decodeString(Constants.MmkvConst.TOKEN),
            10
        )
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<NumberBean?> {
                override fun onSubscribe(d: Disposable) {}
                override fun onNext(numberBean: NumberBean?) {
                    val data: String? = numberBean?.getData()
                    var decryptStr: String? = null
                    try {
                        decryptStr = AESUtil.aesDecryptString(data, Constants.MmkvConst.KEY)
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
                    LogUtils.i("decryptStr900", decryptStr)
                    val gson = Gson()
                    val randomonlinebean = gson.fromJson(
                        decryptStr,
                        RandomOnlineBean::class.java
                    )
                    if (randomonlinebean.code == 0) {
                        if (randomonlinebean.data.users != null) {
                                Glide.with(MyAppliaction.getContext())
                                    .load(Constants.MmkvConst.DOMAIN + MmkvUtil.decodeString("usericon"))
                                    .apply(RequestOptions.bitmapTransform(CircleCrop())) //设置圆形头像
                                    .into(mViewBinding.imageView8)

                            if (randomonlinebean.data.users.size > 0){
                                val icon2 = randomonlinebean.data.users[0].icon
                                Glide.with(MyAppliaction.getContext())
                                    .load(Constants.MmkvConst.DOMAIN + icon2)
                                    .apply(RequestOptions.bitmapTransform(CircleCrop())) //设置圆形头像
                                    .into(mViewBinding.imageView9)
                            }
                            if (randomonlinebean.data.users.size > 1) {
                                val icon3 = randomonlinebean.data.users[1].icon
                                Glide.with(MyAppliaction.getContext())
                                    .load(Constants.MmkvConst.DOMAIN + icon3)
                                    .apply(RequestOptions.bitmapTransform(CircleCrop())) //设置圆形头像
                                    .into(mViewBinding.imageView10)

                            }
                            if (randomonlinebean.data.users.size > 2) {
                                val icon4 = randomonlinebean.data.users[2].icon
                                Glide.with(MyAppliaction.getContext())
                                    .load(Constants.MmkvConst.DOMAIN + icon4)
                                    .apply(RequestOptions.bitmapTransform(CircleCrop())) //设置圆形头像
                                    .into(mViewBinding.imageView11)

                            }

                            if (randomonlinebean.data.users.size > 3) {
                                val icon5 = randomonlinebean.data.users[3].icon
                                Glide.with(MyAppliaction.getContext())
                                    .load(Constants.MmkvConst.DOMAIN + icon5)
                                    .apply(RequestOptions.bitmapTransform(CircleCrop())) //设置圆形头像
                                    .into(mViewBinding.imageView12)

                            }

                        }
                        lifecycleScope.launch {
                            delay(Random.nextLong(2000, 8000))
                            if (randomonlinebean.data.users.isEmpty()) {
                                val intent = Intent(
                                    this@OnlineMatchingActivity,
                                    OnlineMatchingFailureActivity::class.java
                                )

                                startActivity(intent)
                                finish()
                                return@launch
                            }
                            val intent = Intent(
                                this@OnlineMatchingActivity,
                                OnlineMatchingSuccessActivity::class.java
                            )

                            val index = Random.nextInt(0, randomonlinebean.data.users.size)
                            intent.putExtra("userIdcall", randomonlinebean.data.users[index].userId)
                            intent.putExtra(
                                "userIdcallname",
                                randomonlinebean.data.users[index].name
                            )
                            intent.putExtra(
                                "userIdcallprovince",
                                randomonlinebean.data.users[index].province
                            )
                            intent.putExtra(
                                "userIdcallicon",
                                randomonlinebean.data.users[index].icon

                            )

                            startActivity(intent)
                            finish()
                        }

                    }


                }

                override fun onError(e: Throwable) {}
                override fun onComplete() {}


            })
    }

    override fun initListener() {}
}